package com.vhuomai.wechat.support;

import com.quanyue.auto.cache.core.Cache;
import com.vhuomai.wechat.constant.WxConstant;
import com.vhuomai.wechat.handler.*;
import com.vhuomai.weixin.common.api.WxConsts;
import com.vhuomai.weixin.mp.api.WxMpConfigStorage;
import com.vhuomai.weixin.mp.api.WxMpMessageRouter;
import com.vhuomai.weixin.mp.api.WxMpService;
import com.vhuomai.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties(WxProperties.class)
public class WechatConfiguration {

    @Bean
    public WxMpConfigStorage configStorage() {
        return new MultiWxMpConfigStorage();
    }
    
    @Bean
    public Cache wxCache(WxProperties properties) {
        Cache cache = new Cache();
        cache.setKey(WxConstant.WX_TOKEN_CACHE);
        cache.setExpireAfterWrite(properties.getCacheTimeout(), TimeUnit.SECONDS);
        return cache;
    }

    @Bean
    public WxMpService wxMpService(WxMpConfigStorage configStorage) {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(configStorage);
        return wxMpService;
    }

    @Bean
    @ConditionalOnMissingBean
    public MessageHandler messageHandler() {
        return new MessageHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public SubscribeHandler subscribeHandler() {
        return new SubscribeHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public MenuHandler menuHandler() {
        return new MenuHandler();
    }

    @Bean
    public WxMpMessageRouter router(WxMpService wxMpService,
                                    SubscribeHandler subscribeHandler,
                                    MenuHandler menuHandler,
                                    MessageHandler messageHandler) {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

        // 自定义菜单事件
        newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
            .event(WxConsts.BUTTON_CLICK).handler(menuHandler).end();

        // 关注事件
        newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
            .event(WxConsts.EVT_SUBSCRIBE).handler(subscribeHandler)
            .end();

        // 取消关注事件
        newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
            .event(WxConsts.EVT_UNSUBSCRIBE)
            .handler(new UnsubscribeHandler()).end();

        // 扫码事件
        newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
            .event(WxConsts.EVT_SCAN).handler(new ScanHandler()).end();

        // 默认
        newRouter.rule().async(false).handler(messageHandler).end();

        return newRouter;
    }
}
